/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.*;
import midlet.MidletPlay;
import pojo.ImageChicken;
import thread.RunableChickenMove;
import thread.RunableItemsFall;
import thread.threadTime;

/**
 * @author LTTDNQ
 */
public class MIDPCanvasPlay extends Canvas {

    /**
     * constructor
     */
    //Chú ý, độ dài ở đây cho tỉ lệ theo khung hình 240x320,
    //Tao bien 
    //Số gia cầm đang có, tối đa thêm vào 6 con
    private int _intKeyCode = 0;
    private int _chieuDai = getWidth();
    private int _chieuCao = getHeight();
    private int _soItems = 1;
    private int _demThread = 1;
    private int _toaDoRoX = _chieuDai / 2 - 20;
    private int _toaDoTrung = 110;
    private int _soGiaCam = 1;
    private int _life = 3;
    private int _score = 0;
    private int _level = 10;
    private boolean _flagDiTrai = false, _flagDiPhai = false, _stop = false;
    private ImageChicken[] _arrImage = new ImageChicken[6];
    private MidletPlay _midlet;
    private threadTime _threadTime;
    private RunableItemsFall[] _arrItems;
    private RunableChickenMove _Chicks;
    //Toa do x cua diem truoc do
    private int _previousItems;
    //Khởi tạo contrustor cho lớp MIDPCanvasPlay

    public MIDPCanvasPlay(MidletPlay midlet) {
        try {
            this._midlet = midlet;
            //Mặc định ảnh đầu tiên là con gà và set vị trí đầu tiên
            _arrImage[0] = new ImageChicken(Image.createImage("/Images/ga.png"));
            _arrImage[0].setX(_toaDoRoX);
            //Cho tối đa 20 vật phẩm rơi xuống
            //Tạo luồng cho trứng rơi/gà chuyển động và chạy luồng
            _arrItems = new RunableItemsFall[20];
            _previousItems = -1;
            _threadTime = new threadTime(this);
            new Thread(_threadTime).start();
            _Chicks = new RunableChickenMove(this, _arrImage, 105);
            new Thread(_Chicks).start();
            //Chạy luồng
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //Khởi tạo get,set

    public int getSoItems() {
        return _soItems;
    }

    public void setSoItems(int x) {
        this._soItems = x;
    }

    public int getDemThread() {
        return _demThread;
    }

    public void setDemThread(int x) {
        this._demThread = x;
    }

    public int getToaDoTrung() {
        return _toaDoTrung;
    }

    public void setToaDoTrung(int x) {
        this._toaDoTrung = x;
    }

    public int getToaDoRoX() {
        return _toaDoRoX;
    }

    public void setToaDoRoY(int x) {
        this._toaDoRoX = x;
    }

    public int getSoGiaCam() {
        return _soGiaCam;
    }

    public void setSoGiaCam(int x) {
        this._soGiaCam = x;
    }

    public int getLife() {
        return _life;
    }

    public void setLife(int x) {
        this._life = x;
    }

    public int getScore() {
        return _score;
    }

    public void setScore(int x) {
        this._score = x;
    }

    public int getLevel() {
        return _level;
    }

    public void setLevel(int x) {
        this._level = x;
    }

    public void setArrRunableItemsFall(RunableItemsFall[] x) {
        this._arrItems = x;
    }

    public RunableItemsFall[] getArrRunableItemsFall() {
        return this._arrItems;
    }

    public int getPreviousItems() {
        return _previousItems;
    }

    public void setPreviousItems(int x) {
        this._previousItems = x;
    }

    public boolean getStop() {
        return _stop;
    }

    public void setStop(boolean _stop) {
        this._stop = _stop;
    }
    //Tạo luồng trứng

    public int getChieuDai() {
        return _chieuDai;
    }

    public int getChieuCao() {
        return _chieuCao;
    }

    public ImageChicken[] getArrImage() {
        return _arrImage;
    }

    public void setArrImage(ImageChicken[] x) {
        this._arrImage = x;
    }
    //Vẽ quả trứng đang rơi
    private void taoThreadItemsFall(Graphics g) {
        if (_soItems > 0) {
            for (int i = 0; i < _soItems; i++) {
                if (_arrItems[i] != null) {
                    _arrItems[i].paintItem(g);

                }
            }
        }
    }

    public MidletPlay getMidlet() {
        return _midlet;
    }

    /**
     * paint
     */
    //Them mot con ga, vit, ngang
    //Toa do x = (l-30*n)/(1+n)
    public void themImage(Graphics g) throws IOException {
        if (_soGiaCam > 1) {
            _toaDoTrung = (240 - 20 * _soGiaCam) / (1 + _soGiaCam);
            if (_intKeyCode == 52) {
                _arrImage[_soGiaCam - 1] = new ImageChicken(Image.createImage("/Images/ga.png"));
            } else if (_intKeyCode == 54) {
                _arrImage[_soGiaCam - 1] = new ImageChicken(Image.createImage("/Images/vit.png"));
            } else {
                _arrImage[_soGiaCam - 1] = new ImageChicken(Image.createImage("/Images/ngan.png"));
            }
        }
        _Chicks.paintChicken(g);

    }

    public void diChuyenRo(Image img, Graphics g) {
        if (_flagDiTrai || _flagDiPhai) {
            //Gioi han cho phep di chuyen sang left lần cuối
            if (_flagDiTrai && _toaDoRoX >= 20) {
                _toaDoRoX = _toaDoRoX - 20;
                g.drawImage(img, _toaDoRoX, _chieuCao - 40, Graphics.LEFT | Graphics.BOTTOM);
                _flagDiTrai = false;
            } 
            //Giới hạn di chuyển bên phải
            else if (_flagDiPhai && _toaDoRoX <= 180) {
                _toaDoRoX = _toaDoRoX + 20;
                g.drawImage(img, _toaDoRoX*_chieuDai/240, _chieuCao - 40, Graphics.LEFT | Graphics.BOTTOM);
                _flagDiPhai = false;
            } 
            //Ở hai đầu
            else {
                g.drawImage(img, _toaDoRoX*_chieuDai/240, _chieuCao - 40, Graphics.LEFT | Graphics.BOTTOM);
                _flagDiTrai = false;
                _flagDiPhai = false;
            }
        } 
        //Mặc định vị trí rổ lúc đầu
        else {
            g.drawImage(img, _toaDoRoX*_chieuDai/240, _chieuCao - 40, Graphics.LEFT | Graphics.BOTTOM);
        }
    }
    // Vẽ lớp canvas
    public void paint(Graphics graphics) {
        //Kết thúc game
        if (_life < 1) {
            _midlet.vaoManHinhKetThuc();
            return;
            
        } else {
            graphics.setColor(0, 255, 0);
            Image imgBG;
            try {
                imgBG = Image.createImage("/Images/imgBackground.png");
                graphics.drawImage(imgBG, 0, 0, Graphics.LEFT | Graphics.TOP);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
            graphics.setFont(font);
            graphics.setColor(255, 0, 0);
            //Hai dòng mạng và điểm
            graphics.drawString("LIFE : " + _life, 50*_chieuDai/240, 0, Graphics.LEFT | Graphics.TOP);
            graphics.drawString("SCORE : " + _score, 180*_chieuDai/240, 0, Graphics.RIGHT | Graphics.TOP);
            graphics.setStrokeStyle(Graphics.DOTTED);
            graphics.setColor(0x00AAAAAA);
            graphics.drawLine(0, 60*_chieuCao/320, _chieuDai, 60*_chieuCao/320);
            graphics.setStrokeStyle(Graphics.DOTTED);
            graphics.setColor(0x00AAAAAA);

            graphics.drawLine(0, _chieuCao - 30*_chieuCao/320, 240, _chieuCao - 30*_chieuCao/320);
            try {
                themImage(graphics);
                Image imgRo = Image.createImage("/Images/Ro.png");
                diChuyenRo(imgRo, graphics);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            taoThreadItemsFall(graphics);
        }
    }

    public void chayLuongRoiVat() {
        _demThread++;
//        synchronized (this) {
//            _demThread++;
//            notifyAll();
//        }
    }

    public void dungLuongRoiVat() {
        _demThread--;
//        synchronized (this) {
//            _demThread--;
//            notifyAll();
//        }
    }

    public void destroy() {
        _stop = true;
        _threadTime = null;
        _arrItems = null;
        _Chicks = null;
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
        //52 : NUM4(G), 56 NUM8(V), 54 NUM6(N), -3 LEFT, -4 RIGHT, 9 GAME_A, 10 GAME_B, 11 GAME_C, 12 GAME_D
        //System.out.println(" KEY A " + GAME_A + " KEY B " + GAME_B + " KEY C" + GAME_C + " KEY D" + GAME_D);
        switch (keyCode) {
            case -3:
                _flagDiTrai = true;
                repaint(0, _chieuCao - 40,_chieuDai,_chieuCao-40);
                break;
            case -4:
                _flagDiPhai = true;
                repaint(0, _chieuCao - 40,_chieuDai,_chieuCao-40);
                break;
            //Them ga
                
            case 52:
                if (_soGiaCam < 6) {
                    _intKeyCode = 52;
                    _soGiaCam++;
                    repaint();
                    break;
                }
            case 9:
                if (_soGiaCam < 6) {
                    _intKeyCode = 52;
                    _soGiaCam++;
                    repaint();
                    break;
                }
            //Them vit
            case 56:
                if (_soGiaCam < 6) {
                    _intKeyCode = 56;
                    _soGiaCam++;
                    repaint();
                    break;
                }
            case 11:
                if (_soGiaCam < 6) {
                    _intKeyCode = 56;
                    _soGiaCam++;
                    repaint();
                    break;
                }    
            //Them ngang     
            case 54:
                if (_soGiaCam < 6) {
                    _intKeyCode = 54;
                    _soGiaCam++;
                    repaint();
                    break;
                }
            case 10:
                if (_soGiaCam < 6) {
                    _intKeyCode = 54;
                    _soGiaCam++;
                    repaint();
                    break;
                }    
        }


    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
        if (x < _toaDoRoX) {
            _flagDiTrai = true;
            repaint();
        } else {
            _flagDiPhai = true;
            repaint();
        }

    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }
    /**
     * Called when action should be handled
     */
}
